from django.shortcuts import render
from rest_framework.viewsets import ModelViewSet,GenericViewSet
from rest_framework.generics import ListAPIView, RetrieveAPIView, CreateAPIView
from .recruit_serializers.city_serializers import *
from .recruit_serializers.recruit_serializers import *
from .recruit_serializers.enterprise_serializers import *
from rest_framework.response import Response
from rest_framework.views import APIView


# Create your views here.
# 热门城市
class HotCity(ListAPIView):
    serializer_class = HotCitySerializers
    queryset = City.objects.filter(ishot=1)


# 最新职位，推荐职位
class NewRecruits(ListAPIView):
    queryset = Recruit.objects.all()
    serializer_class = NewRecruitSerializer


# 热门企业
class HotEnterprise(ListAPIView):
    serializer_class = HotEnterpriseSerializer
    queryset = Enterprise.objects.all()


# 职位详情
class RecruitsLists(ModelViewSet):
    serializer_class = RecruitDetailSerializer

    def get_queryset(self):
        id = self.kwargs.get('pk')
        if id is None:
            return Recruit.objects.all()
        else:
            return Recruit.objects.filter(id=id)


# 企业访问次数,增加企业访问次数
class NumberEnterprise(ModelViewSet):
    serializer_class = HotEnterpriseSerializer
    queryset = Enterprise.objects.all()

    def update(self, request, *args, **kwargs):
        pk = self.kwargs['pk']
        a = self.queryset.get(pk=pk)
        a.visits += 1
        a.save()
        return Response({"message": "更新成功", "success": True})


# 职位访问次数
class RecruitAccessNumber(ModelViewSet):
    serializer_class = RecruitAccessNumberSerializer
    queryset = Recruit.objects.all()

    def update(self, request, *args, **kwargs):
        pk = self.kwargs['pk']
        a = self.queryset.get(pk=pk)
        a.visits += 1
        a.save()
        return Response({"message": "更新成功", "success": True})


# 企业详情
class EnterpriseLists(ModelViewSet):
    serializer_class = EnterDetailSerializer

    def get_queryset(self):
        id = self.kwargs.get('pk')
        if id is None:
            return Enterprise.objects.all()
        else:
            return Enterprise.objects.filter(id=id)


# 职位收藏
class RecruitCollect(APIView):
    def post(self, request, pk):

        user = request.user
        recruit_user = Recruit.objects.get(pk=pk)
        users = recruit_user.users.all()
        for user in users:
            if user.id == pk:
                recruit_user.users.remove(user.id)
        recruit_user.users.add(user.id)
        return Response({"message": "更新成功", "success": True})


# 企业收藏
class EnterCollect(APIView):
    def post(self, request, pk):

        user = request.user
        enterprise_user = Enterprise.objects.get(pk=pk)
        users = enterprise_user.users.all()
        for user in users:
            if user.id == pk:
                enterprise_user.users.remove(user.id)
        enterprise_user.users.add(user.id)
        return Response({"message": "更新成功", "success": True})


# 搜索职位
class SearchRecruit(ModelViewSet):
    serializer_class = NewRecruitSerializer

    def get_queryset(self):

        city = self.request.data.get('cityname')
        keyword = self.request.data.get('keyword')
        if keyword is None:
            return Recruit.objects.filter(city=city)
        else:
            return Recruit.objects.filter(city=city).filter(jobname__contains=keyword)
























